package com.xjj.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author zhanghejie
 * @date 2020/7/26 17:41
 */
//https://blog.csdn.net/qq_29479041/article/details/83826550
@RestController
public class EchoController {//ClientController

    private final RestTemplate restTemplate;

    @Autowired
    public EchoController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @RequestMapping(value = "/echo", method = RequestMethod.GET)
    public String echo(@RequestParam String name, ServerHttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        return restTemplate.getForObject("http://order-service/test1?name=" + name, String.class);
    }
    //1.第一种方式(直接使用restTemplate，url写死)
//        RestTemplate restTemplate = new RestTemplate();
//        String response = restTemplate.getForObject("http://localhost:8888/msg", String.class);
    //2.弟二种方式(利用loadBalancerClient通过应用名获取url,然后再使用直接使用restTemplate)
//        ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
//        String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort())+"/msg";
//        RestTemplate restTemplate = new RestTemplate();
//        String response = restTemplate.getForObject(url, String.class);
//
    //3.弟二种方式(利用@LoadBalanced，可在restTemplate里使用应用名字)
    // orderApplication
    //    @LoadBalanced
    //    @Bean
    //    public RestTemplate restTemplate() {
    //        return new RestTemplate();
    //    }
    // String response = restTemplate.getForObject("http://PRODUCT/msg", String.class);

}
